dat.data(dat.data > 900.)=NaN;
dat.data(dat.data < 0.)=NaN;

nmodel.data(nmodel.data > 500.)=NaN;

mi=min(nanmin(nanmin(dat.data)),nanmin(nanmin(nmodel.data)));
ma=max(nanmax(nanmax(dat.data)),nanmax(nanmax(nmodel.data)));

switch parameter
    case 'T'
        md=5;
    case 'S'
        md=1;
        mi=35.5;
        ma=37.5;
    case 'NO3'
        mi=0;
        ma=10;
        md=5;
    case 'CHL'
        md=1;
    case 'PON'
        mi=0;
        ma=4;
        md=5;
    case 'DON'
        md=2;
    otherwise
        md=2*nanstd(nanstd(abs(nmodel.data-dat.data)));
end

if strcmp(parameter,'T') || strcmp(parameter,'S')
    X=repmat(dat.timedata,1,length(dat.depthdata));
    XX=repmat(nmodel.time,1,length(nmodel.depth));
    Y=repmat(dat.depthdata,1,length(dat.timedata))';
    YY=repmat(nmodel.depth,1,length(nmodel.time))';
    X=reshape(X,1,size(X,1)*size(X,2));
    Y=reshape(Y,1,size(Y,1)*size(Y,2));
    XX=reshape(XX,1,size(XX,1)*size(XX,2));
    YY=reshape(YY,1,size(YY,1)*size(YY,2));
    Z1=reshape(dat.data,1,size(dat.data,1)*size(dat.data,2));
    Z2=reshape(nmodel.data,1,size(nmodel.data,1)*size(nmodel.data,2));
    Z3=reshape((nmodel.data-dat.data),1,size(nmodel.data,1)*size(nmodel.data,2));
elseif strcmp(parameter,'NO3') || strcmp(parameter,'CHL') || strcmp(parameter,'PON') || ...
        strcmp(parameter,'DON')
    X=repmat(dat.timedata,length(dat.depthdata),1)';
    XX=repmat(nmodel.time,length(nmodel.depth),1)';
    Y=repmat(dat.depthdata,length(dat.timedata),1);
    YY=repmat(nmodel.depth,length(nmodel.time),1);
    X=reshape(X,size(X,1)*size(X,2),1);
    Y=reshape(Y,size(Y,1)*size(Y,2),1);
    XX=reshape(XX,size(XX,1)*size(XX,2),1);
    YY=reshape(YY,size(YY,1)*size(YY,2),1);
    Z1=reshape(dat.data,size(dat.data,1)*size(dat.data,2),1);
    Z2=reshape(nmodel.data,size(nmodel.data,1)*size(nmodel.data,2),1);
    Z3=reshape((nmodel.data-dat.data),size(nmodel.data,1)*size(nmodel.data,2),1);
end



X2=XX(~isnan(Z2));
Y2=YY(~isnan(Z2));
Z2=Z2(~isnan(Z2));
Z3=Z3(~isnan(Z1));
X3=X(~isnan(Z1));
Y3=Y(~isnan(Z1));
X1=X(~isnan(Z1));
Y1=Y(~isnan(Z1));
Z1=Z1(~isnan(Z1));


% ---- data
close all
figure
h=scatter(X1,...
    -Y1,40,...
    Z1);
set(h,'Marker','o','MarkerFaceColor','flat')
set(gca,'XTick',datenum(1997,1:3:12,1),...
    'TickLength',[0.005 0.025],...
    'XMinorTick','on',...
    'YLim',[-300 0])
datetick('x',28,'keepticks','keeplimits')
%shading flat
caxis([mi ma])
%xlabel('Time')
title(['Data ',kind,' ',parameter])
set(gca,'TickDir','out')
colorbar
printgc('-f1','-depsc2',['./PLOT_VALIDATION/BATS/BATS',bbio,'_data_',parameter,'.eps'])

%return

% ---- model
close all
figure
h=scatter(X2,...
    -Y2,40,...
    Z2);
set(h,'Marker','o','MarkerFaceColor','flat')
%whos
%pcolor(X2,-Y2,Z2)
%shading flat
set(gca,'XTick',datenum(1997,1:3:12,1),...
    'TickLength',[0.005 0.025],...
    'XMinorTick','on',...
    'YLim',[-300 0])
datetick('x',28,'keepticks','keeplimits')
%shading flat
caxis([mi ma])
xlabel('Time')
title(['Model ',kind,' ',parameter])
set(gca,'TickDir','out')
colorbar
printgc('-f1','-depsc2',['./PLOT_VALIDATION/BATS/BATS',bbio,'_model_',parameter,'.eps'])


% Ne fonctionne seulement si temps modele = temps donnees

% % ---- model-data
% close all
% figure
% h=scatter(X3,...
%     -Y3,40,...
%     Z3);
% set(h,'Marker','o','MarkerFaceColor','flat')
% set(gca,'XTick',datenum(1997,1:3:12,1),...
%     'TickLength',[0.005 0.025],...
%     'XMinorTick','on',...
%     'YLim',[-300 0])
% datetick('x',28,'keepticks','keeplimits')
% %shading flat
% caxis([-md md])
% xlabel('Time')
% title(['(Model-Data) ',kind,' ',parameter])
% set(gca,'TickDir','out')
% load cmap_politoliu
% colormap(politoliu)
% colorbar
% printgc('-f1','-depsc2',['./PLOT_VALIDATION/BATS/BATS',bbio,'_diff_',parameter,'.eps'])
% close all